package com.example.luyang7415445.ly_15716test;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView {

	private View contentView;
	public interface MyScrollViewListener {
		void onScrollChanged(MyScrollView myScrollView, int x, int y, int oldx,
							 int oldy);
	}

	public static interface OnBorderListener {

        /**
         * Called when scroll to bottom
         */
        public void onBottom();

        /**
         * Called when scroll to top
         */
        public void onTop();
    }

	MyScrollViewListener myScrollViewListener;
	OnBorderListener onBorderListener;

	public MyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public MyScrollView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public void setMyScrollViewListener(
			MyScrollViewListener myScrollViewListener) {
		this.myScrollViewListener = myScrollViewListener;

	}
	
	 public void setOnBorderListener(final OnBorderListener onBorderListener) {
	        this.onBorderListener = onBorderListener;
	        if (onBorderListener == null) {
	            return;
	        }

	        if (contentView == null) {
	            contentView = getChildAt(0);
	        }
	    }

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		super.onScrollChanged(l, t, oldl, oldt);
		if (myScrollViewListener != null) {
			myScrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
		}
	
		   if (contentView != null && contentView.getMeasuredHeight() <= getScrollY() + getHeight()) {
	            if (onBorderListener != null) {
	                onBorderListener.onBottom();
	            }
	        } else if (getScrollY() == 0) {
	            if (onBorderListener != null) {
	                onBorderListener.onTop();
	            }
	        }
	}

	@Override
	public void onScreenStateChanged(int screenState) {
		super.onScreenStateChanged(screenState);
	}
}
